package org.jeecg.modules.performance.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonArray;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IContentInfoService;
import org.jeecg.modules.performance.service.ICalculatePerforService;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.jeecg.modules.publish.entity.ThirdContentLink;
import org.jeecg.modules.publish.entity.ThirdReadNum;
import org.jeecg.modules.publish.service.IThirdContentLinkService;
import org.jeecg.modules.publish.service.IThirdReadNumService;
import org.jeecg.modules.publish.util.AccessTokenUtil;
import org.jeecg.util.DateTimeUtil;
import org.jeecg.util.WeiBoUtil;
import org.jeecgframework.p3.core.util.HttpUtils;
import org.jeewx.api.core.common.util.WeixinUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class PerformanceWeinXinReadJob implements Job {
    //
    @Autowired
    private IThirdContentLinkService thirdContentLinkService;
    @Autowired
    private IThirdReadNumService thirdReadNumService;
    @Autowired
    private IContentInfoService contentInfoService;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String startTime = DateTimeUtil.getLastDayCharN8En(30 );
        String endTime = DateTimeUtil.getLastDayCharN8En(1 );
        QueryWrapper<ThirdContentLink> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("data",startTime);
        queryWrapper.le("data",endTime);
        queryWrapper.eq("type",1);
        List<ThirdContentLink> list = thirdContentLinkService.list(queryWrapper);


        List<ThirdReadNum> thirdReadNumList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            queryWrapper.isNotNull("weixin_key");
            queryWrapper.groupBy("weixin_key");

            List<ThirdContentLink> keyList = thirdContentLinkService.list(queryWrapper);

            for (ThirdContentLink key : keyList) {
             //   Map<String,ThirdContentLink> map = list.stream().collect(Collectors.toMap(ThirdContentLink::getThirdContentId, x->x));
                String weixinToken =  AccessTokenUtil.getAccseeToken(key.getWeixinKey(), key.getWeixinSecret());

                String url ="https://api.weixin.qq.com/datacube/getarticlesummary?access_token="+weixinToken;
                HashMap<String,String> paramMap = new HashMap<>();
                paramMap.put("begin_date",endTime);
                paramMap.put("end_date",endTime);
                String result = HttpUtils.doPostJson(url,JSONObject.toJSONString(paramMap));
                JSONArray jsonArray = JSON.parseObject(result).getJSONArray("list");
                for (Object o : jsonArray) {
                    JSONObject detail = JSON.parseObject(o.toString());
                    Integer count=  detail.getInteger("int_page_read_user");
                    Integer shareCount = detail.getInteger("share_user");
                    Integer loveCount = detail.getInteger("add_to_fav_user");
                    String thirdId = detail.getString("msgid").split("_")[0];
                    String title=  detail.getString("title");

                    for (ThirdContentLink thirdContentLink : list) {
                        ContentInfo contentInfo = contentInfoService.getById(thirdContentLink.getContentId());
                        if(contentInfo.getContentTitle().equals(title)&&thirdId.equals(thirdContentLink.getId())){
                            //  ThirdContentLink thirdContentLink = map.get(thirdId);
                            ThirdReadNum thirdReadNum = new ThirdReadNum();
                            thirdReadNum.setReadNum(count);
                            thirdReadNum.setContentId(thirdContentLink.getContentId());
                            thirdReadNum.setLoveNum(loveCount);
                            thirdReadNum.setRepostsNum(shareCount);
                            thirdReadNum.setType(1);
                            thirdReadNumList.add(thirdReadNum);
                            break;
                        }
                    }





                }

            }

            if(!CollectionUtils.isEmpty(thirdReadNumList)){
                thirdReadNumService.saveBatch(thirdReadNumList);
            }

            }



    }

    public static void main(String[] args) {
        String weixinToken =  AccessTokenUtil.getAccseeToken("wxaff0e76caad8a3c", "4b31f5f9ebeb6fefd9bd5b9e17e67db9");

        String url ="https://api.weixin.qq.com/datacube/getarticlesummary?access_token="+weixinToken;
        HashMap<String,String> paramMap = new HashMap<>();
        paramMap.put("begin_date","2021-08-18");
        paramMap.put("end_date","2021-08-18");
        String result = HttpUtils.doPostJson(url,JSONObject.toJSONString(paramMap));
        JSONArray jsonArray = JSON.parseObject(result).getJSONArray("list");
        System.out.println(result);
    }
}
